package me.leiusual.sharing.ui.product

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Transformations.switchMap
import androidx.lifecycle.ViewModel
import me.leiusual.sharing.api.model.ProductDetail
import me.leiusual.sharing.repository.ProductRepository
import me.leiusual.sharing.repository.model.Data
import me.leiusual.sharing.util.extension.toLiveData
import javax.inject.Inject

/**
 * author: leiusual
 * create on: 2019-07-26
 * description:
 */
class ProductDetailViewModel @Inject constructor(
    private val productRepository: ProductRepository
) : ViewModel() {

  // 详情

  private val _productDetail = MutableLiveData<ParamsProductDetail>()
  val productDetail: LiveData<Data<ProductDetail>> = switchMap(_productDetail) {
    productRepository.productDetail(it.aid, it.mid, it.r).toLiveData()
  }

  fun productDetail(aid: Long?, mid: Long, r: Long?) {
    _productDetail.value = ParamsProductDetail(aid, mid, r)
  }

  data class ParamsProductDetail(val aid: Long?, val mid: Long, val r: Long?)
}